package com.centanet.framework.network;

import com.centanet.framework.interfaces.ILoggerTag;
import com.centanet.framework.interfaces.IParamsEncode;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import java.io.UnsupportedEncodingException;

/**
 * json格式请求参数格式化
 */
public class JsonEncode implements IParamsEncode {

    private final Gson gson = new Gson();
    private final Object object;

    public JsonEncode(Object object) {
        this.object = object;
    }

    @Override
    public byte[] encode(String paramsEncoding) {
        try {
            final String params = gson.toJson(object);
            Logger.t(ILoggerTag.FRAME_REQUEST).d("params : %s", params);
            return params.getBytes(paramsEncoding);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Encoding not supported: " + paramsEncoding, e);
        }
    }
}
